// <copyright file="PageRepository.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Cms.Data
{
    using System;
    using System.Linq;
    using System.Data;
    using System.Data.Entity;
    using System.Collections.Generic;

    using CleverCMS.Cms.Data.Entities;

    public class PageRepository : CmsGenericRepository<CmsPage>, IPageRepository
    {
        private readonly ICmsGenericRepository<CmsTag> m_tag;
        public PageRepository(CmsDataContext ctx, ICmsGenericRepository<CmsTag> tag): base(ctx)
        {
            m_tag = tag;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IQueryable<CmsPage> LoadAllPublished()
        {
            return LoadAll(filter: x => x.StatusId == (int)StatusCode.Published);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="numberOfLastCreated"></param>
        /// <returns></returns>
        public IQueryable<CmsPage> LoadLast(int numberOfLastCreated)
        {
            return LoadAll(filter: x => x.StatusId == (int)StatusCode.Published, orderBy: q => q.OrderByDescending(d => d.CreatedAt)).Take(numberOfLastCreated);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        public void Save(CmsPage page)
        {
            //TODO: Review tags logic
            //page.Tags.Where(x => x.Id != 0).ToList().ForEach(x => context.Entry<CmsTag>(x).State = EntityState.Unchanged);
            base.Save(page, page.Id == 0);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CmsPage Load(int id)
        {
            var page = base.Load(id);
            return page;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="alias"></param>
        /// <returns></returns>
        public CmsPage LoadByAlias(string alias)
        {
            return base.LoadAll(x => x.Alias == alias).SingleOrDefault();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            base.Delete(id);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tagName"></param>
        /// <returns></returns>
        public CmsTag LoadTag(string tagName)
        {
            return m_tag.LoadAll(filter: x => x.Name == tagName).FirstOrDefault();
        }

    }
}